﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ALC.Core.Enums;
using ALC.Core.ViewModels.Workpiece;
using WPFCommon.Helpers;

namespace ALC.Views.Fai
{
    public partial class WorkpieceDictionaryView : UserControl
    {

        private bool _hasInit = false;
        private static readonly string[] WorkpiecePropNames=  typeof(WorkpieceDictionaryViewModel).GetProperties().Select(prop => prop.Name).ToArray();
        private  WorkpieceDictionaryViewModel _viewModel;
        private WorkpieceView[] _workpieceViews;


        public WorkpieceDictionaryView()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty WorkpieceDictProperty = DependencyProperty.Register(
            "WorkpieceDict", typeof(WorkpieceDictionaryViewModel), typeof(WorkpieceDictionaryView), new PropertyMetadata(default(WorkpieceDictionaryViewModel), OnWorkpieceDictPropertyChanged));



        private static void OnWorkpieceDictPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = (WorkpieceDictionaryViewModel)e.NewValue;
            var view = (WorkpieceDictionaryView) d;
            if (viewModel == null || view._hasInit) return;
            view._hasInit = true;
            view._viewModel = viewModel;
            
            // Set bindings for all WorkpieceViewModel props
            var allCavities = EnumHelper.GetValues<CavityType>().ToArray();
            view._workpieceViews = new WorkpieceView[allCavities.Length];
            
            for (var index = 0; index < allCavities.Length; index++)
            {
                var cavity = allCavities[index];
                // Set binding to WorkpieceViewModel
                var bindingPropName = GetWorkpiecePropName(cavity);
                var binding = new Binding()
                {
                    Source = viewModel, Path = new PropertyPath(bindingPropName),
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };
                var workpieceView = new WorkpieceView();
                BindingOperations.SetBinding(workpieceView, WorkpieceView.DataContextProperty, binding);

                // Add WorkpieceView to Ui
                view.PART_MainGrid.ColumnDefinitions.Add(new ColumnDefinition()
                    {Width = new GridLength(1, GridUnitType.Star)});
                Grid.SetColumn(workpieceView, index);
                view.PART_MainGrid.Children.Add(workpieceView);
                
                // Add WorkpieceView to cache
                view._workpieceViews[index] = workpieceView;
            }
            
        }

      
        public WorkpieceDictionaryViewModel WorkpieceDict
        {
            get { return (WorkpieceDictionaryViewModel) GetValue(WorkpieceDictProperty); }
            set { SetValue(WorkpieceDictProperty, value); }
        }


        private static string GetWorkpiecePropName(CavityType cavityType)
        {
            var alikePropNames = WorkpiecePropNames.Where(name => name.Contains(cavityType.ToString())).ToArray();
            if (alikePropNames.Length == 1) return alikePropNames[0];
            if(alikePropNames.Length == 0) throw new KeyNotFoundException($"Can not find a prop with name that contains {cavityType}");

            var possibleProInfos = typeof(WorkpieceDictionaryViewModel).GetProperties()
                .Where(info => alikePropNames.Any(name => info.Name.Contains(name)));

            return possibleProInfos.Where(info => info.PropertyType == typeof(WorkpieceViewModel))
                .Select(info => info.Name).First();
        }
        
    }
}